package com.example.qimowork.Fragments;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.qimowork.R;
import com.example.qimowork.ShuJu.Data;
import com.example.qimowork.ShuJu.MyJson;
import com.example.qimowork.adapters.MyRecyclerViewAdapter;
import com.example.qimowork.adapters.ShouYeAdapter;
import com.example.qimowork.util.HttpUtil;
import com.google.gson.Gson;

import org.json.JSONException;

import java.io.IOException;
import java.util.List;

public class ShouYeFragment extends Fragment implements ViewPager.OnPageChangeListener{
    private View view;
    private RecyclerView recyclerView;
    private ListView lv;
    private ShouYeAdapter adapter;
    private MyRecyclerViewAdapter recyclerViewAdapter;
    private List<Data> dataList,list;
    private HttpUtil httpUtil;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            list = (List<Data>) msg.obj;
            adapter = new ShouYeAdapter(getActivity(),list);
            lv.setAdapter(adapter);
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view==null) {
            view = inflater.inflate(R.layout.fragment_shouye, container, false);
            initView();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent!=null){
            parent.removeView(view);
        }
        return view;
    }

    private void initView() {
        httpUtil = new HttpUtil();

        recyclerView = view.findViewById(R.id.shouye_recyclerView);
        lv = view.findViewById(R.id.shouye_lv);

        new Thread() {
            public void run() {
                try {
                    String json = httpUtil.connectServer("top");
                    Gson gson = new Gson();
                    MyJson myJson = gson.fromJson(json, MyJson.class);
                    dataList = myJson.getResult().getData();
                    Message message = new Message();
                    message.obj = dataList;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        recyclerViewAdapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }
        });
        recyclerView.setAdapter(recyclerViewAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        recyclerViewAdapter.setBg(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
